package com.ngc.mall.service.impl;

import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.domain.AttachmentDomain;
import com.ngc.mall.domain.CategoryDomain;
import com.ngc.mall.dto.AttachmentDTO;
import com.ngc.mall.dto.CategoryDTO;
import com.ngc.mall.dto.FileInfoDTO;
import com.ngc.mall.mapper.CategoryMapper;
import com.ngc.mall.service.IAttachmentService;
import com.ngc.mall.service.ICategoryService;
import com.ngc.mall.service.ICosService;
import com.ngc.mall.vo.CategoryVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends BaseServiceImpl<CategoryMapper, CategoryDomain> implements ICategoryService {
    @Resource
    private IAttachmentService iAttachmentService;
    @Resource
    private ICosService iCosService;
    @Override
    public List<CategoryVO> selectAllCategory() {
        List<CategoryDomain> categoryDomainList =super.list();
        List<CategoryVO> spuCategoryVOList = categoryDomainList.stream().map(categoryDomain -> {
            return CategoryVO
                    .builder()
                    .name(categoryDomain.getName())
                    .img(categoryDomain.getImg())
                    .isEnabled(categoryDomain.getIsEnabled())
                    .sort(categoryDomain.getSort())
                    .id(categoryDomain.getId())
                    .pid(categoryDomain.getPid())
                    .build();
        }).collect(Collectors.toList());

        return spuCategoryVOList;
    }

    @Override
    public Long createCategory(CategoryDTO dto){
        CategoryDomain domain= CategoryDomain
                .builder()
                .name(dto.getName())
                .isEnabled(dto.getIsEnabled())
                .pid(dto.getPid())
                .sort(dto.getSort())
                .img(iCosService.getObjectUrl(dto.getImg()))
                .build();
        Long id=super.create(domain);
        return id;
    }

    @Override
    public Boolean deleteById(Long id) {
        List<Long> ids=new ArrayList<>();
        List<Long> childrenIds = baseMapper.findCategoryIdsByPid(id);
        ids.add(id);
        ids.addAll(childrenIds);
        return super.removeByIds(ids);
    }


    /**
     * 通过fileName向COS查找线上图片地址，
     * @param attachmentDTO
     * @param id
     * @return
     */
    public AttachmentDomain getAttachmentInfo(AttachmentDTO attachmentDTO, Long id){
        FileInfoDTO fileInfoDTO=iCosService.getFormalObject(attachmentDTO.getFileName());
        AttachmentDomain attachmentDomain=AttachmentDomain
                .builder()
                .recordId(id)
                .sort(attachmentDTO.getSort())
                .fileSize(fileInfoDTO.getFileSize())
                .filePath(fileInfoDTO.getFileUrl())
                .type(6)
                .fileType(getFileType(attachmentDTO.getFileName()))
                .build();
        return attachmentDomain;
    }

    public String getFileType(String fileName){
        String fileType=fileName.substring(fileName.lastIndexOf('.'));
        return fileType;
    }


}
